<template>
  <el-dialog :title="!dataForm.liveId ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="直播名"
                    prop="liveName">

        <el-input v-model="dataForm.liveName"
                  placeholder="直播名"></el-input>
      </el-form-item>

      <el-form-item label="房屋"
                    prop="newHouseIds">
        <el-form :inline="true"
                 :model="newHouse"
                 @keyup.enter.native="getDataList()">
          <el-form-item>
            <el-input v-model="newHouse.key"
                      placeholder="参数名"
                      clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
           
          </el-form-item>
        </el-form>
        <el-table :data="dataList"
                  border
                  v-loading="dataListLoading"
                  @selection-change="selectionChangeHandle"
                  style="width: 100%;">
          <el-table-column type="selection"
                           header-align="center"
                           align="center"
                           width="50">
          </el-table-column>
          <el-table-column prop="houseName"
                           header-align="center"
                           align="center"
                           label="房屋名">
          </el-table-column>
          <el-table-column prop="houseAddress"
                           header-align="center"
                           align="center"
                           label="地址">
          </el-table-column>
          <el-table-column prop="houseArea"
                           header-align="center"
                           align="center"
                           label="面积（m2)-">
          </el-table-column>
          <el-table-column prop="housePrice"
                           header-align="center"
                           align="center"
                           label="金额（/平方）">
            <template slot-scope="scope">
              <el-input v-model="scope.row.housePrice"
                        placeholder="活动价"
                        clearable></el-input>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination @size-change="sizeChangeHandle"
                       @current-change="currentChangeHandle"
                       :current-page="pageIndex"
                       :page-sizes="[5, 10, 20, 30]"
                       :page-size="pageSize"
                       :total="totalPage"
                       layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-form-item>

      <el-form-item label="开始时间"
                    prop="startDate">
        <!-- <el-input v-model="dataForm.startDate" placeholder="开始时间"></el-input> -->
        <el-date-picker v-model="dataForm.startDate"
                        type="datetime"
                        placeholder="开始时间"
                        value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>

      </el-form-item>
      <el-form-item label="结束时间"
                    prop="endDate">
        <!-- <el-input v-model="dataForm.endDate" placeholder="结束时间"> </el-input>-->
        <el-date-picker v-model="dataForm.endDate"
                        type="datetime"
                        placeholder="结束时间"
                        value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="拉流"
                    prop="upUrl">
        <el-input v-model="dataForm.upUrl"
                  placeholder="拉流"></el-input>
      </el-form-item>
      <el-form-item label="推流"
                    prop="downUrl">
        <el-input v-model="dataForm.downUrl"
                  placeholder="推流"></el-input>
      </el-form-item>
      <el-button type="primary"
                 @click="tuila()">点击生成拉流推流</el-button>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        liveId: 0,
        liveName: '',
        startDate: '',
        endDate: '',
        upUrl: '',
        downUrl: '',
        liveHouses: []
      },
      newHouse: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      dataRule: {
        liveName: [
          { required: true, message: '直播名不能为空', trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/house/newhouse/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.newHouse.key
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      let houseList = []
      for (const key in val) {
        houseList.push({
          'houseId': val[key].houseId,
          'price': val[key].housePrice
        })
      }
      this.dataForm.liveHouses = houseList
      console.log(val, houseList)
    },
    init (id) {
      this.dataForm.liveId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.liveId) {
          this.$http({
            url: this.$http.adornUrl(`/live/livestream/info/${this.dataForm.liveId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.liveName = data.liveStream.liveName
              this.dataForm.startDate = data.liveStream.startDate
              this.dataForm.endDate = data.liveStream.endDate
              this.dataForm.upUrl = data.liveStream.upUrl
              this.dataForm.downUrl = data.liveStream.downUrl
              this.dataList = data.liveStream.newHouseVos
            }
          })
        } else {
          this.getDataList()
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/live/livestream/${!this.dataForm.liveId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'liveId': this.dataForm.liveId || undefined,
              'liveName': this.dataForm.liveName,
              'startDate': this.dataForm.startDate,
              'endDate': this.dataForm.endDate,
              'upUrl': this.dataForm.upUrl,
              'downUrl': this.dataForm.downUrl,
              'liveHouses': this.dataForm.liveHouses
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    tuila () {
      this.$http({
        url: this.$http.adornUrl(`/live/livestream/saveLive`),
        method: 'get',
        params: ({
          'sourceId': 1
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data)
          this.dataForm.upUrl = data.pushUrl
          this.dataForm.downUrl = data.pullUrl
        } else {

        }
      })
    },
    // getnewHouseList () {
    //   // 获取数据列表
    //   this.dataListLoading = true
    //   this.$http({
    //     url: this.$http.adornUrl('/house/newhouse/duoxuan'),
    //     method: 'get'
    //   }).then(({ data }) => {
    //     if (data && data.code === 0) {
    //       this.newHouseList = data.page
    //       console.log(data)
    //     } else {
    //       this.newHouseList = []
    //     }
    //     this.dataListLoading = false
    //   })
    // }

  },
  created () {
  }
}
</script>
